package com.ys.filelib.filedialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.ys.filelib.R;

public abstract class MyDialog {

    private final Dialog mDialog;
    private final Context mContext;
    private final Handler mHandler = new Handler();

    public MyDialog(Context context) {
        mContext = context;
        mDialog = new Dialog(context, R.style.panel_dialog) {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                View root = getView();
                if (root != null) {
                    setContentView(root);
                }
            }

            @Override
            protected void onStart() {
                super.onStart();
                MyDialog.this.onStart();
            }

            @Override
            protected void onStop() {
                super.onStop();
                MyDialog.this.onStop();
            }
        };
        mDialog.setCanceledOnTouchOutside(false);
    }

    protected View getView() {
        return null;
    }

    protected void onStart() {

    }

    protected void onStop() {

    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    public void setCancelable(boolean flag) {
        mDialog.setCancelable(flag);
    }

    public Dialog getDialog() {
        return mDialog;
    }

    public Window getWindow() {
        return mDialog.getWindow();
    }

    public Context getContext() {
        return mContext;
    }

    public void runOnUI(Runnable runnable) {
        mHandler.post(runnable);
    }

    public LayoutInflater getInflater() {
        return LayoutInflater.from(mContext);
    }
}
